<template>
  <!-- 待分配宿舍的人员列表 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-form-item label="工号" prop="jobNumber">
          <el-input v-model="queryParams.jobNumber" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="姓名" prop="fullName">
          <el-input v-model="queryParams.fullName" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="部门" prop="department">
          <treeselect ref="tree" :options="deptOptions" :show-count="true" style="width: 260px" placeholder="请选择归属部门"
            @select="selectDepart" />
        </el-form-item> -->
        <el-form-item label="岗位" prop="position">
          <el-input v-model="queryParams.position" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="福建籍管理员" prop="fujianmanage" label-width="100px">
          <el-select v-model="queryParams.fujianmanage" placeholder="请选择">
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请状态" prop="allocated" label-width="100px">
          <el-select v-model="queryParams.allocated" placeholder="请选择">
            <el-option label="待办" value="待办"></el-option>
            <el-option label="已办" value="已办"></el-option>
            <el-option label="驳回" value="驳回"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <!-- <el-button icon="el-icon-search" size="mini" @click="advancedsearchOpen = true">高级搜索</el-button> -->
          <el-button type="primary" plain size="mini" @click="handleAssign" :disabled="multiple"
            v-hasPermi="['business:tobeallocated:assign']">分配宿舍</el-button>
        </el-form-item>
      </el-row>
    </el-form>


    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工号" align="center" prop="jobNumber" />
      <el-table-column label="姓名" align="center" prop="fullName" />
      <el-table-column label="电话" align="center" prop="contactNumber" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="岗位" align="center" prop="position" />
      <el-table-column label="申请状态" align="center" prop="allocated">
        <template slot-scope="scope">
          <el-tag :type="scope.row.allocated === '待办' ? 'danger' : 'success'"
            disable-transitions>{{ scope.row.allocated }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="房间号" align="center" prop="beddetail" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160px" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-refresh-left" v-if="scope.row.allocated == '待办'"
            @click="handleBack(scope.row)">驳回</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!--分派宿舍对话框 -->
    <el-dialog :title="assigntitle" :visible.sync="assignopen" width="1000px" append-to-body>
      <el-form ref="assignform" :model="assignform" label-width="130px">
        <el-cascader-panel v-model="assignform.bedIds" :props="props" :options="options"></el-cascader-panel>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="assignbuttonLoading" type="primary" @click="assignsubmitForm">确 定</el-button>
        <el-button @click="assigncancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="员工信息详情" :visible.sync="detailopen" width="1300px" :key="Math.random()" append-to-body>
      <detail :rowData="dynamicData"></detail>
    </el-dialog>


  </div>
</template>
  
<script>
import {
  listEmployee,
  getEmployee,
  editAllocatedToReject
} from "@/api/business/employee";
import { getbedtree, assignDormitory } from "@/api/business/dormitory";
import { deptTreeSelect } from "@/api/system/user";
import Detail from "@/views/business/employee/detail";

export default {
  name: "Tobeallocated",

  components: { Detail },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobNumber: undefined,
        fullName: undefined,
        department: undefined,
        position: undefined,
        delFlag: "0",

        allocated: "待办,已办,驳回"
      },

      // 部门树选项
      deptOptions: undefined,
      // 岗位选项
      postOptions: [],

      assignbuttonLoading: false,
      //床位
      assignform: {
        employeeIds: null,
        bedIds: null,
      },
      // 弹出层标题
      assigntitle: "",
      // 是否显示弹出层
      assignopen: false,
      props: { multiple: true },
      options: [],


      //员工列表数据
      dynamicData: [],
      detailopen: false,

    };
  },
  created() {
    this.getList();
    // this.getDeptTree();
  },
  methods: {

    /** 查询员工列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then((response) => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        delFlag: "0",
        allocated: "待办,已办,驳回"
      };
      this.queryParams.department = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },



    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    DRHA_EFaultModeTree_handleSelect(node, instanceId) {
      this.form.department = node.label;
    },
    /** 分派宿舍按钮 */
    handleAssign() {
      const ids = this.ids;
      this.assignform.employeeIds = ids;
      (this.assignform.bedIds = null), (this.assignopen = true);
      this.assigntitle = "分派宿舍";
      this.GetbedTree();
    },
    assigncancel() {
      (this.assignform.bedIds = null), (this.assignopen = false);
      //console.log(this.assignform);
    },
    /** 分派宿舍提交事件 */
    assignsubmitForm() {
      this.$refs["assignform"].validate((valid) => {
        if (valid) {
          //this.assignbuttonLoading = true;

          assignDormitory(this.assignform)
            .then((response) => {
              this.$modal.msgSuccess("分派成功");
              this.assignopen = false;
              this.getList();
            })
            .finally(() => {
              this.assignbuttonLoading = false;
            });
        }
      });
    },
    //获取床位的tree树
    GetbedTree() {
      let data = { outsource: "否" };
      getbedtree(data).then((response) => {
        this.options = response;
        //console.log(response)
      });
    },


    handleDetail(row) {
      getEmployee(row.id).then((response) => {
        this.dynamicData[0] = response.data;
        this.detailopen = true;
      });
    },

    handleBack(row) {
      this.$modal.confirm('是否确认驳回？').then(() => {
        this.loading = true;
        editAllocatedToReject(row.id).then((response) => {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        })
          .finally(() => {
            this.loading = false;
          });
      })
        .then(() => {
          this.loading = false;
          this.getList();

        })
        .catch(() => { })
        .finally(() => {
          this.loading = false;
        });

    }

  },
};
</script>
  
<style scoped>
.el-cascader-panel {
  height: 60vh;

  .el-scrollbar__wrap {
    height: 100%;
  }
}

.base-info-box {
  overflow: hidden;
}
</style>